<template>
  <div>
    <el-form :rules="rules"
             v-loading="loading"
             element-loading-text="正在登录..."
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)"
             ref="loginForm"
             :model="loginForm"
             class="loginContainer">
      <h3 class="loginTitle">系统登陆</h3>
      <el-form-item prop="username">
        <el-input type="text" clearable auto-complete="false" v-model="loginForm.username"
                  placeholder="请输入用户名"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" clearable auto-complete="false" v-model="loginForm.password"
                  placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item prop="code">
        <el-input type="text" clearable auto-complete="false" v-model="loginForm.code" placeholder="点击图片更换验证码"
                  style="width: 250px;margin-right: 5px"></el-input>
        <img :src="captchaUrl" @click="updateCaptcha">
      </el-form-item>
      <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
      <br>
      <el-button type="primary" style="width: 40%" @click="submitLogin">登录</el-button>
      <el-button type="primary" style="width: 40%" @click="goToRegis">注册</el-button>
    </el-form>
  </div>
</template>

<script>
import qs from "qs";

export default {
  name: "Login",
  data() {
    return {
      captchaUrl: '/api/checkCode?time=' + new Date(),
      loginForm: {
        username: 'xuxiao',
        password: '123',
        code: ''
      },
      loading: false,
      checked: true,
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}],
        code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = '/api/checkCode?time=' + new Date();
    },
    submitLogin() {
      var data = qs.stringify({
        'username': this.loginForm.username,
        'password': this.loginForm.password,
        'code': this.loginForm.code,
      });
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;//登录加载动画开启
          this.postRequest('/api/login', data).then(resp => {
            console.log(resp)
            if (resp.code == 1) {
              //存储用户token
              const tokenStr = resp.data;
              window.sessionStorage.setItem('token', tokenStr);
              //跳转首页
              let path = this.$route.query.redirect;
              this.$router.replace((path == '/' || path == undefined) ? '/home' : path);
              this.getRequest(`/api/user/name/${this.loginForm.username}`).then(resp => {
                if (resp.code == 1) {
                  let data = resp.data
                  console.log(resp.data)
                  window.localStorage.setItem("user", JSON.stringify(data));
                }
              })
              this.$message.success("欢迎来到银行管理系统")

            } else {
              if (resp.code == 0) {
                this.$message.error("验证码错误")
                this.loading = false;
              } else {
                this.$message.error("账户或密码错误")
                this.loading = false;
              }
            }
          })
        } else {
          this.$message.error('请输入所有字段！');
          return false;
        }
      });
    },
    goToRegis() {
      this.$router.push("/register")
    }
  }

}
</script>

<style scoped>
.loginContainer {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 15px 35px 15px 35px;
  background: #ffffff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.loginTitle {
  margin: 0 auto 40px auto;
  text-align: center;
}

.loginRemember {
  text-align: left;
  margin: 0px 0px 15px 0px;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

</style>
